from PyQt5.QtWidgets import *
from PyQt5.QtGui import*
from PyQt5 import*
from PyQt5.QtCore import *
import  sys
import cgitb
cgitb.enable(format="text")
class MyButton(QLabel):
    #自定义信号
    clicked=pyqtSignal()

    def __init__(self,*args,parent=None):
        super().__init__(parent)
        self.hoverPixmap=QPixmap(args[0])
        self.normalPixmap=QPixmap(args[1])
        self.pressPixmap=QPixmap(args[2])

        #标记鼠标的位置，True表示鼠标在按钮上面
        self.enterState=False

        self.setPixmap(self.normalPixmap)
        self.setFixedSize(self.normalPixmap.size())
    def mouseReleaseEvent(self, ev: QtGui.QMouseEvent):
        if self.enterState:
            self.setPixmap(self.pressPixmap)
        else:
            self.setPixmap(self.normalPixmap)

        #发射信号
        self.clicked.emit()
    def mousePressEvent(self, ev: QtGui.QMouseEvent):
        self.setPixmap(self.pressPixmap)
    #鼠标移入
    def enterEvent(self, a0: QtCore.QEvent):
        self.setPixmap(self.hoverPixmap)
    #鼠标离开
    def leaveEvent(self, a0: QtCore.QEvent):
        if self.enterState:
            self.setPixmap(self.pressPixmap)
        else:
            self.setPixmap(self.normalPixmap)
#测试
'''
if __name__=="__main__":
    a=QApplication(sys.argv)
    myButton=MyButton("./source/人机对战_hover.png","./source/人机对战_press.png","./source/人机对战_normal.png")
    myButton.show()
    sys.exit(a.exec_())
'''